package gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import client.ClientConsole;

import logic.Message;
import logic.PdfFile;
import logic.Title;

/**
 * Shows a add book form and sends user's input.
 * Adds a new book with details inserted
 */
public class AddBookForm extends JPanel implements ActionListener{
	/** JPanel used to arrange GUI design*/
	private JPanel mainPanel,bookFormPanel,buttonPanel,backbottom;
	private JTextField name,author,edition,printDate,subject,purchaseDate,location,copies;
	private JLabel nameLabel,authorLabel,editionLabel,printDateLabel,wantedLabel,
	subjectLabel,purchaseDateLabel,locationLabel,descriptionLabel,errorLabel,copiesLabel,fileLabel;
	/** buttons the user can press- add book, cancel-clear text fields,browse
	 * for a PDF.
	 */
	private JButton back,add,cancel, browse;
	private JTextPane description;
	/** if selected the book is wanted, otherwise not wanted*/
	private JCheckBox wanted;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	/** hold all information for a new book to be added*/
	private Title editedTitle=new Title();
	
	
	/**
	 * Default constructor-Builds the add book form
	 */
	public AddBookForm()
	{
		super();
		setSize(300, 300);
		setLayout(new BorderLayout());
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		bookFormPanel=new JPanel();
		bookFormPanel.setLayout(new GridBagLayout());
		gbc.insets= new Insets(7,7,7,7);
		nameLabel=new JLabel("Name: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(nameLabel,gbc);
		name=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(name,gbc);
		authorLabel=new JLabel("Author: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(authorLabel,gbc);
		author=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=1;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(author,gbc);
		printDateLabel=new JLabel("Print Date: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=4;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(printDateLabel,gbc);
		printDate=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=4;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(printDate,gbc);
		editionLabel=new JLabel("Edition: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(editionLabel,gbc);
		edition=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=3;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(edition,gbc);
		subjectLabel=new JLabel("Subject: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(subjectLabel,gbc);
		subject=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=2;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(subject,gbc);
		purchaseDateLabel=new JLabel("Purchase Date: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=5;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(purchaseDateLabel,gbc);
		purchaseDate=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=5;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(purchaseDate,gbc);
		locationLabel=new JLabel("Location: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=6;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(locationLabel,gbc);
		location=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=6;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(location,gbc);
		copiesLabel=new JLabel("Copies: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=7;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(copiesLabel,gbc);
		copies=new JTextField(15);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=7;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(copies,gbc);
		wantedLabel=new JLabel("Wanted: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=8;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(wantedLabel,gbc);
		wanted=new JCheckBox();
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=8;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(wanted,gbc);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=9;
		gbc.insets= new Insets(4,4,4,4);
		descriptionLabel=new JLabel("Description: ");
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=10;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(descriptionLabel,gbc);
		description=new JTextPane();
		description.setSize(50, 50);
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=10;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(description,gbc);
		buttonPanel= new JPanel();
		add=new JButton("Add");
		cancel=new JButton("Cancel");
		final JFileChooser fc = new JFileChooser();
		browse = new JButton("Browse");
		buttonPanel.add(add);
		buttonPanel.add(browse);
		buttonPanel.add(cancel);
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=11;
		gbc.gridwidth=2;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(buttonPanel,gbc);
		fileLabel=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=12;
		gbc.gridwidth=2;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(fileLabel,gbc);
		fileLabel.setVisible(false);
		errorLabel=new JLabel();
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=13;
		gbc.gridwidth=2;
		gbc.insets= new Insets(4,4,4,4);
		bookFormPanel.add(errorLabel,gbc);
		errorLabel.setVisible(false);
		mainPanel.add(bookFormPanel,BorderLayout.CENTER);
		back=new JButton("back");
		backbottom=new JPanel();
		backbottom.add(back);
		back.addActionListener(this);
		back.addActionListener(this);
		add.addActionListener(this);
		cancel.addActionListener(this);
		mainPanel.add(backbottom,BorderLayout.SOUTH);
		add(mainPanel,BorderLayout.CENTER);
		
		browse.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int returnVal = fc.showOpenDialog(AddBookForm.this);
				 
	            if (returnVal == JFileChooser.APPROVE_OPTION) {
	                File file = fc.getSelectedFile();
	                fileLabel.setText(file.getName());
	                fileLabel.setVisible(true);
	          	  byte [] fileData = new byte[(int)file.length()];
	          	  FileInputStream d = null;
	          	  try {
	          		
	          		d = new FileInputStream(file);
	          		DataInputStream dis = new DataInputStream (d);
	          		dis.readFully(fileData);
	          		dis.close();
	          	} catch (FileNotFoundException e1) {
	          		e1.printStackTrace();
	          	}
	          	   
	          	   catch (IOException e1) {
	          		e1.printStackTrace();
	          	} 
	          	editedTitle.setTableOfContents(new  PdfFile (file.getName(),(int)file.length(),fileData));
	                
	            }  
	            }
		});
		
	}
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if( ae.getSource() == add )
		{
			if (author.getText().isEmpty() || name.getText().isEmpty()|| description.getText().isEmpty() || subject.getText().isEmpty()|| location.getText().isEmpty()
					|| edition.getText().isEmpty()|| printDate.getText().isEmpty() || purchaseDate.getText().isEmpty()|| copies.getText().isEmpty())
			{
				errorLabel.setVisible(true);
				errorLabel.setText("Please fill all fields");
			}
			else
			{
				SimpleDateFormat dateFormat=new SimpleDateFormat("dd/MM/yyyy");
				
				editedTitle.setAuthor(author.getText());
				editedTitle.setName(name.getText());
				editedTitle.setDescription(description.getText());
				editedTitle.setSubject(subject.getText());
				editedTitle.setShelfLocation(location.getText());
				editedTitle.setEditionNum(edition.getText());
				editedTitle.setCopies(Integer.parseInt(copies.getText()));
				try {
					editedTitle.setPrintDate(dateFormat.parse(printDate.getText()));
					System.out.println(dateFormat.parse(printDate.getText()));
					editedTitle.setPurchaseDate(dateFormat.parse(purchaseDate.getText()));
				} catch (ParseException e) {
					System.out.printf("Error parsing date");
				}
				if (wanted.isSelected()==true)
					editedTitle.setWanted(true);
				else
					editedTitle.setWanted(false);
				
				Message m=new Message("Add Book",editedTitle);
				ClientConsole.HandleMessageFromGui(m);
				m=(Message)ClientConsole.client.getMessage();
				if(m.getCommand().equals("Book Added"))
				{
					errorLabel.setText("Added successfully");
					errorLabel.setVisible(true);
				}
				else
				{
					errorLabel.setText(m.getCommand());
					errorLabel.setVisible(true);
					
				}
			}
			
		}
		else if ( ae.getSource() == cancel )
		{
			name.setText("");
			subject.setText("");
			description.setText("");
			author.setText("");
			edition.setText("");
			printDate.setText("");
			purchaseDate.setText("");
			location.setText("");
			copies.setText("");
			wanted.setSelected(false); 
			
		}
		else if ( ae.getSource() == back )
		{
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new InventoryManagmentWindow());	
		}
		
	}
}
